﻿using GameAnalyticsSDK;
using HWGames.Bundles.Internal;
using HWGames.Bundles.Internal.Analytics;
using System.Collections.Generic;
//using UnityEditor.Build;
//using UnityEditor.Build.Reporting;

public static class SDKBundle {

    public const string Version = "1.4.3";
    public const float PRICE_MUL = 0.8f; //内购系数

    private static int maxEventCount = 4;


    private static SDKBundleSettings _sdkSettings = null;
    public static SDKBundleSettings SDKSettings {
        get {
            if (_sdkSettings == null) {
                _sdkSettings = SDKBundleSettings.Load();
            }
            return _sdkSettings;
        }
    }

#if UNITY_EDITOR
    private static VideoSetting _videoSettting = null;
    public static VideoSetting VideoSettings {
        get {
            if (_videoSettting == null) {
                _videoSettting = VideoSetting.Load();
            }
            return _videoSettting;
        }
    }
#endif

    /// <summary>
    /// 游戏第几关/等级，worldID 世界ID, level 关卡/等级，需要记录0级数据
    /// </summary>
    public static void GameLevelEvent(int worldID, int level) {
        AnalyticsManager.TrackCustomEvent($"level:{worldID}:{level}");
    }

    /// <summary>
    /// 生产线解锁事件 worldID 世界ID, index 产线编号，需要记录0级数据
    /// </summary>
    public static void GameProductionEvent(int worldID, int index) {
        AnalyticsManager.TrackCustomEvent($"production:{worldID}:{index}");
    }

    /// <summary>
    /// 游戏开始事件，level，没有可传null
    /// </summary>
    public static void OnGameStarted(string level) {
        AnalyticsManager.TrackCustomEvent($"OnGameStarted:{level}");
    }

    /// <summary>
    /// 游戏过关事件，level为当前关卡，没有可传null,score 为过关分数
    /// </summary>
    public static void GameSuccess(string level, int score = 0) {
        AnalyticsManager.TrackCustomEvent($"GameSuccess:{level}:{score}");
    }
    /// <summary>
    /// 游戏失败事件，level为当前关卡，没有可传null,score 为过关分数
    /// </summary>
    /// <param name="level">The level number of the game</param>
    public static void GameFailed(string level, int score = 0) {
        AnalyticsManager.TrackCustomEvent($"GameFailed:{level}:{score}");
    }

    /// <summary>
    /// 传自定义事件,参数内不能包含除"_"外的特殊符号,且单个参数字符串长度不得16个字符
    /// </summary>
    public static void TrackCustomEvent(string eventName, Dictionary<string, object> eventProperties = null) {
        AnalyticsManager.TrackCustomEvent(eventName, eventProperties);
    }

    /// <summary>
    /// 传自定义事件,_objs最多只能有四个参数,参数内不能包含除"_"外的特殊符号,
    /// 且单个参数字符串长度不得16个字符
    /// </summary>
    public static void TrackCustomEvent(string eventName, params object[] _objs) {
        if (_objs.Length > maxEventCount) {
            HWLog.LogE("Event Error", $"More than  {maxEventCount} events count, please use other methods!");
        }
        AnalyticsManager.TrackCustomEvent(eventName, _objs);
    }

#if UNITY_PURCHASING
    /// <summary>
    /// 传内购事件
    /// </summary>
    /// <param name="currency">货币类型</param>
    /// <param name="defaultPrice">默认货币价格</param>
    /// <param name="amount">货币价格</param>
    /// <param name="itemType">内购类型</param>
    /// <param name="itemId">内购ID</param>
    /// <param name="cartType">内购的UI界面名称</param>
    public static void TrackPurchaseEvent(UnityEngine.Purchasing.Product product, float defaultPrice, string itemType, string cartType) {
        /// 传内购事件给GA
        AnalyticsManager.TrackPurchaseEvent(product.metadata.isoCurrencyCode, (int)((double)(product.metadata.localizedPrice * 100) * PRICE_MUL), itemType, product.definition.id, cartType);
        HWGames.Bundles.Internal.Analytics.SingularMgr.Instance.InAppPurchase(product, null);
    }
#endif

    /// <summary>
    /// Facebook事件,如无特殊要求不要调用
    /// </summary>
    public static void TrackFaceBookCustomEvent(string eventName, Dictionary<string, object> eventProperties = null) {
        AnalyticsManager.TrackFaceBookCustomEvent(eventName, eventProperties);
    }

    /// <summary>
    /// Firebase事件,如无特殊要求不要调用,Lite未实现
    /// </summary>
    public static void TrackFirebaseCustomEvent(string eventName, Dictionary<string, object> eventProperties = null) {
        AnalyticsManager.TrackFireBaseCustomEvent(eventName, eventProperties);
    }

    /// <summary>
    /// Singular事件，如无特殊要求不要调用
    /// </summary>
    public static void TrackSingularCustomEvent(string eventName, Dictionary<string, object> eventProperties = null) {
        AnalyticsManager.TrackSingularCustomEvent(eventName, eventProperties);
    }

    public static bool IsEnableAndroidBannerAd() {
        return SDKSettings.enableBanner;
    }

    public static bool IsEnableIOSBannerAd() {
        return SDKSettings.enableIOSBanner;
    }

    public static bool IsEnableOpenIntervalTime() {
        return SDKSettings.enableOpenIntervalTime;
    }

    public static float GetFirstOpenInterIntervalTime() {
        return SDKSettings.firstOpenInterstitialIntervalTime;
    }

    public static float GetOpenInterIntervalTime() {
        return SDKSettings.OpenInterstitialIntervalTime;
    }

    public static float GetInterIntervalTime() {
        return SDKSettings.interstitialIntervalTime;
    }

    public static string GetRewardUnitID() {
#if UNITY_IOS
        return SDKSettings.iOSRewardAdUnitID;
#else
        return SDKSettings.androidRewardAdUnitID;
#endif
    }

    public static string GetInterUnitID() {
#if UNITY_IOS
        return SDKSettings.iOSInterstitialAdUnitID;
#else
        return SDKSettings.androidInterstitialAdUnitID;
#endif
    }

    public static string GetBannerUnitID() {
#if UNITY_IOS
        return SDKSettings.iOSBannerAdUnitID;
#else
        return SDKSettings.androidBannerAdUnitID;
#endif
    }

    public static string GetFacebookID() {
        return SDKSettings.facebookAppId;
    }

    public static string GetSingularAPIKey() {
        return SDKSettings.SingularAPIkey;
    }

    public static string GetSingularSecretkey() {
        return SDKSettings.SingularSecretkey;
    }

#if UNITY_EDITOR
    public static bool IsShowVideo() {
        return VideoSettings.ShowVideo;
    }
#endif

    public enum AnalyticsProvider {
        HWAnalytics,
        GameAnalytics,
    }
}
